package diandiansong.library.utils;

import android.os.CountDownTimer;
import android.widget.TextView;

/**
 * Created by act on 2016/3/5.
 * timecount in textview
 */
public class TimeCount extends CountDownTimer {

    //static=================================

    public static void timerStart(long millisInFuture, long countDownInterval, TextView tv, String textEnd, String textTimeUnit) {
        TimeCount timeCount = new TimeCount(millisInFuture, countDownInterval, tv, textEnd, textTimeUnit);
        timeCount.start();
    }

    //static=================================

    private TextView textView;
    private String textEnd;
    private String textTimeUnit;

    public TimeCount(long millisInFuture, long countDownInterval, TextView textView, String textEnd, String textTimeUnit) {
        super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        this.textView = textView;
        this.textTimeUnit = textTimeUnit;
        this.textEnd = textEnd;
    }

    @Override
    public void onFinish() {// 计时完毕时触发
        textView.setText(textEnd);
        textView.setClickable(true);
    }

    @Override
    public void onTick(long millisUntilFinished) {// 计时过程显示
        textView.setClickable(false);
        textView.setText(String.format("%d%s", millisUntilFinished / 1000, textTimeUnit));
    }
}